const isWebsite = require('@/validator/isWebsite');

/**
 * 获取url后面的传值
 * @param {String} url 网址地址或者一个query string数据
 * @returns {Object} 返回一个JSON数据
 */
module.exports = function getQueryVariable(url) {
  let search = '';
  if (!url.includes('?') || !isWebsite(url)) {
    search = url;
  } else {
    search = url.split('?').pop();
    if (!search) return {};
  }
  const queries = search.split('&');
  const results = {};
  for (const item of queries) {
    const [key = '', value = ''] = item.split('=');
    results[key.trim()] = value.trim();
  }
  return results;
};
